﻿<UserControl x:Class="Tum.PDE.ToolFramework.Modeling.Visualization.Controls.ErrorListControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" TextOptions.TextFormattingMode="Display" 
             d:DesignHeight="300" d:DesignWidth="700"
             xmlns:ctrl="clr-namespace:Tum.PDE.ToolFramework.Modeling.Visualization.Controls"
             xmlns:converter="clr-namespace:Tum.PDE.ToolFramework.Modeling.Visualization.Converters"
             xmlns:attached="clr-namespace:Tum.PDE.ToolFramework.Modeling.Visualization.Controls.Attached"
            
             xmlns:vma="clr-namespace:Tum.PDE.ToolFramework.Modeling.Visualization.ViewModel.Attached"
             
             xmlns:beh="clr-namespace:Tum.PDE.ToolFramework.Modeling.Visualization.Behaviors"
             xmlns:inter="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             
             vma:ViewModelAttachedCommands.Loaded="{Binding LoadedCommand}"
             vma:ViewModelAttachedCommands.Unloaded="{Binding UnloadedCommand}">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Tum.PDE.ToolFramework.Modeling.Visualization;component/Themes/Default/ErrorListButtonStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="ItemContStyle" TargetType="ListViewItem">
                <Setter Property="attached:CommandBehavior.Event" Value="MouseDoubleClick" />
                <Setter Property="attached:CommandBehavior.Command" Value="{Binding ElementName=ErrorLstView, Path=DataContext.NavigateCommand}" />
                <Setter Property="Padding" Value="0,4" />
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Border.BorderThickness" Value="0,0,0,0.5" />
                <Setter Property="Border.BorderBrush" Value="LightGray" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsFiltered}" Value="True">
                        <Setter Property="Foreground" Value="Gray"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            
            <converter:ICollectionToNumberConverter x:Key="ICollectionToNumberConverter"/>
        </ResourceDictionary>
    </UserControl.Resources>
   <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="28" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
       
       <!-- Display error/warning/info toggle buttons-->
        <StackPanel Orientation="Horizontal" Grid.Row="0" Margin="4,4,0,0" MinHeight="20">
            <ToggleButton x:Name="ButtonError" Style="{DynamicResource ErrorListButtonStyle}" Command="{Binding Path=ToggleErrorCategory}" 
                          Margin="0,0,0,0" IsChecked="{Binding Path=IsErrorCategoryVisible, Mode=OneTime}">
                <StackPanel Orientation="Horizontal" Margin="4,0,4,0">
                    <Image Width="16" Height="16" Source="/Tum.PDE.ToolFramework.Images;component/Views/Remove.ico" />
                    <TextBlock Margin="3,0,0,0" Text="{Binding Path=ErrorsCount}"></TextBlock>
                    <TextBlock Margin="0,0,0,0" Text=" Errors"></TextBlock>
                </StackPanel>
            </ToggleButton>
            <ToggleButton x:Name="ButtonWarning" Style="{DynamicResource ErrorListButtonStyle}" Command="{Binding Path=ToggleWarningCategory}" Margin="4,0,0,0" 
                          IsChecked="{Binding Path=IsWarningCategoryVisible, Mode=OneTime}">
                <StackPanel Orientation="Horizontal" Margin="4,0,4,0">
                    <Image Width="16" Height="16" Source="/Tum.PDE.ToolFramework.Images;component/Views/Warning.ico" />
                    <TextBlock Margin="3,0,0,0" Text="{Binding Path=WarningsCount}"></TextBlock>
                    <TextBlock Margin="0,0,0,0" Text=" Warnings"></TextBlock>
                </StackPanel>
            </ToggleButton>
            <ToggleButton x:Name="ButtonInfo" Style="{DynamicResource ErrorListButtonStyle}" Command="{Binding Path=ToggleMessageCategory}" Margin="4,0,0,0" 
                          IsChecked="{Binding Path=IsMessageCategoryVisible, Mode=OneTime}">
                <StackPanel Orientation="Horizontal" Margin="4,0,4,0">
                    <Image Width="16" Height="16" Source="/Tum.PDE.ToolFramework.Images;component/Views/Info.ico" />
                    <TextBlock Margin="3,0,0,0" Text="{Binding Path=MessagesCount}"></TextBlock>
                    <TextBlock Margin="0,0,0,0" Text=" Messages"></TextBlock>
                </StackPanel>
            </ToggleButton>
            
            <!-- Splitter between normal buttons and filter button -->
            <Border Margin="8,2,8,2" BorderThickness="1" BorderBrush="CadetBlue"/>
            
            <!-- Filter display -->
            <ToggleButton x:Name="ButtonFilter" Style="{DynamicResource ErrorListButtonStyle}" Command="{Binding Path=ToggleFilteredItems}" Margin="0,0,0,0" 
                          IsChecked="{Binding Path=IsFilteredCategoryVisible, Mode=OneTime}">
                <StackPanel Orientation="Horizontal" Margin="4,0,4,0">
                    <Image Width="16" Height="16" Source="/Tum.PDE.ToolFramework.Images;component/Views/Filter.ico" />
                    <TextBlock Margin="3,0,0,0" Text="{Binding Path=FilteredCount}"></TextBlock>
                    <TextBlock Margin="0,0,0,0" Text=" Filtered Messages"></TextBlock>
                </StackPanel>
            </ToggleButton>
        </StackPanel>
       
       <!-- Error list -->
        <ListView x:Name="ErrorLstView" Grid.Row="1" Margin="4" Padding="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                  ItemContainerStyle="{StaticResource ItemContStyle}"
                  ItemsSource="{Binding Path=DisplayedErrorList, Mode=OneWay}" 
                  SelectionMode="Extended" attached:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding Path=SelectedItems}"
                  attached:ListViewSortHelper.EnableSorting="True">

            <inter:Interaction.Behaviors>
                <beh:FrameworkElementContextMenuBehavior/>
                <beh:ListViewColumnWidthBehavior/>
            </inter:Interaction.Behaviors>
 
            <ListView.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Text}"></Setter>
                </Style>
            </ListView.Resources>
            <ListView.ContextMenu>
                <ContextMenu ItemsSource="{Binding Path=MenuOptions}" ItemContainerStyleSelector="{StaticResource ContextMenuItemStyleSelector}" />
            </ListView.ContextMenu>
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="28" Header="" attached:ListViewSortHelper.SortingCommand="{Binding Path=SortByCategoryCommand}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Image Source="/Tum.PDE.ToolFramework.Images;component/Views/Remove.ico" x:Name="ErrorIcon" Visibility="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Center" Width="16" Height="16" />
                                    <Image Source="/Tum.PDE.ToolFramework.Images;component/Views/Warning.ico" x:Name="WarningIcon" Visibility="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Center" Width="16" Height="16" />
                                    <Image Source="/Tum.PDE.ToolFramework.Images;component/Views/Info.ico" x:Name="InfoIcon" Visibility="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Center" Width="16" Height="16" />
                                </Grid>
                                <DataTemplate.Triggers>
                                    <DataTrigger Value="Error" Binding="{Binding Category}">
                                        <Setter Property="Visibility" TargetName="ErrorIcon" Value="Visible" />
                                    </DataTrigger>
                                    <DataTrigger Value="Warning" Binding="{Binding Category}">
                                        <Setter Property="Visibility" TargetName="WarningIcon" Value="Visible" />
                                    </DataTrigger>
                                    <DataTrigger Value="Information" Binding="{Binding Category}">
                                        <Setter Property="Visibility" TargetName="InfoIcon" Value="Visible" />
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>                        
                    </GridViewColumn>
                    <GridViewColumn Header="" Width="20" DisplayMemberBinding="{Binding Path=Number}" attached:ListViewSortHelper.SortingCommand="{Binding Path=SortByNumberCommand}"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Description}" attached:ListViewSortHelper.SortingCommand="{Binding Path=SortByDescriptionCommand}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Description" Margin="2"/>
                                    <Image x:Name="SortingImage" Width="8" Height="8" VerticalAlignment="Center" Margin="2" Source="/Tum.PDE.ToolFramework.Images;component/Views/Down-16.png" Visibility="Collapsed"/>
                                </StackPanel>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding Path=DataContext.SortOrder, ElementName=ErrorLstView}" Value="Description">
                                        <Setter TargetName="SortingImage" Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                                    <MultiDataTrigger>
                                        <MultiDataTrigger.Conditions>
                                            <Condition Binding="{Binding Path=DataContext.SortOrder, ElementName=ErrorLstView}" Value="Description"/>
                                            <Condition Binding="{Binding Path=DataContext.IsSortedAscending, ElementName=ErrorLstView}" Value="False"/>
                                        </MultiDataTrigger.Conditions>
                                        <Setter TargetName="SortingImage" Property="Source" Value="/Tum.PDE.ToolFramework.Images;component/Views/Up-16.png"/>
                                    </MultiDataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="250" DisplayMemberBinding="{Binding Path=SourceDisplayName}" attached:ListViewSortHelper.SortingCommand="{Binding Path=SortBySourceDisplayNameCommand}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Source" Margin="2"/>
                                    <Image x:Name="SortingImage" Width="8" Height="8" VerticalAlignment="Center" Margin="2" Source="/Tum.PDE.ToolFramework.Images;component/Views/Down-16.png" Visibility="Collapsed"/>
                                </StackPanel>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding Path=DataContext.SortOrder, ElementName=ErrorLstView}" Value="SourceDisplayName">
                                        <Setter TargetName="SortingImage" Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                                    <MultiDataTrigger>
                                        <MultiDataTrigger.Conditions>
                                            <Condition Binding="{Binding Path=DataContext.SortOrder, ElementName=ErrorLstView}" Value="SourceDisplayName"/>
                                            <Condition Binding="{Binding Path=DataContext.IsSortedAscending, ElementName=ErrorLstView}" Value="False"/>
                                        </MultiDataTrigger.Conditions>
                                        <Setter TargetName="SortingImage" Property="Source" Value="/Tum.PDE.ToolFramework.Images;component/Views/Up-16.png"/>
                                    </MultiDataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</UserControl>
